use crypto::digest::Digest;
use crypto::md5::Md5;
#[allow(unused_must_use)]
use serde::{Deserialize, Serialize};
use std::sync::mpsc::channel;

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Talker {
    pub iden_id: String,
    pub nick_name: String,
    pub password: String,
    pub email: String,
    pub signature: String,
    pub phone: String,
    pub address: String,
    pub birthday: String,
    pub age: i32,
    pub code: String,
    pub room_id:u16,
    //pub create_at:NaiveDateTime,
}

impl Talker {
    pub fn new() -> Self {
        Talker {
            iden_id: "-".to_string(),
            nick_name: "-".to_string(),
            password: "-".to_string(),
            email: "-".to_string(),
            signature: "-".to_string(),
            phone: "-".to_string(),
            address: "-".to_string(),
            birthday: "1995-10-25 00:00:00".to_string(),
            age: 0,
            code: String::new(),
            room_id:0,
            //create_at:NaiveDateTime::parse_from_str("2020-11-16 00:00:00", "%Y-%m-%d %H:%M:%S").expect("时间转换错误"),
        }
    }
    pub fn set_iden_id(&mut self) {
        let mut hasher = Md5::new();
        hasher.input_str(&self.nick_name);
        self.iden_id = hasher.result_str();
    }
    pub fn set_nick_name(&mut self, name: String) {
        self.nick_name = name.clone();
        let mut hasher = Md5::new();
        hasher.input_str(&name);
        self.iden_id = hasher.result_str();
    }
    pub fn set_email(&mut self, email: String) {
        self.email = email.clone();
        let mut hasher = Md5::new();
        hasher.input_str(&email);
        self.password = hasher.result_str();
    }

    pub fn insert(&self) -> Result<u64, String> {
        let sself = self.clone();
        let (sx, rx) = channel();
        db_exec!(sx,"insert into talkers(iden_id,nick_name,password,signature,phone,address,birthday,email,age) values($1,$2,$3,$4,$5,$6,$7,$8,$9)",
        &[&sself.iden_id,&sself.nick_name,&sself.password,&sself.signature,&sself.phone,&sself.address,&sself.birthday,&sself.email,&sself.age]);
        tokio::spawn(async move {
            //  log_debug_ln!("{}","hello world 2");
            // log_debug_ln!("{}","db client获取成功");
        });
        let res = match rx.recv_timeout(std::time::Duration::from_secs(60)) {
            Err(e) => {
                return Err(e.to_string());
            }
            Ok(o) => o,
        };
        match res {
            Err(e) => Err(e.to_string()),
            Ok(o) => Ok(o),
        }
    }
    pub fn update(&self) -> Result<(), String> {
        Ok(())
    }
    pub fn find_by_email(&mut self) -> Result<(), String> {
        let sself = self.clone();
        let (sx, rx) = channel();
        db_query_one!(sx,"select iden_id,nick_name,signature,phone,address,birthday,age from talkers where email = $1 and password = $2",
        &[&sself.email,&sself.password]);
        tokio::spawn(async move {
            //  log_debug_ln!("{}","hello world 2");
            // log_debug_ln!("{}","db client获取成功");
        });
        let res = match rx.recv_timeout(std::time::Duration::from_secs(3)) {
            Err(e) => {
                return Err(e.to_string());
            }
            Ok(o) => o,
        };
        match res {
            Err(e) => {
                return Err(e.to_string());
            }
            Ok(row) => {
                if row.is_empty() {
                    return Err("查询内容为空".to_string());
                }
                self.iden_id = row.get(0);
                self.nick_name = row.get(1);
                self.signature = row.get(2);
                self.phone = row.get(3);
                self.address = row.get(4);
                self.birthday = row.get(5);
                self.age = row.get(6);
                //self.create_at = row.get(8);
                return Ok(());
            }
        }
        // let mut db = db::get_db()?;
        // match db.query_one("select iden_id,nick_name,signature,phone,address,birthday,age,email from talkers where email = $1 and password = $2",&[&self.email,&self.password]).await{
        //     Err(e)=>{return Err(e.to_string());}
        //     Ok(row)=>{
        //         if row.is_empty() {}
        //         self.iden_id = row.get(0);
        //         self.nick_name = row.get(1);
        //         self.signature = row.get(2);
        //         self.phone = row.get(3);
        //         self.address = row.get(4);
        //         self.birthday = row.get(5);
        //         self.age = row.get(6);
        //         self.email = row.get(7);
        //         //self.create_at = row.get(8);
        //     }
        // }
        //Ok(())
    }
}

#[test]
fn test_hash() {
    let mut t = Talker::new();
    t.set_email("1".to_string());
    println!("哈希内容 {:?}", t.password);
}
